<template>
  <el-dialog title="分配权限" width="500px" :visible.sync="isShow">
    <!-- 选项勾选功能
el-tree
    show-checkbox:显示复选框
    node-key:设置每条数据选中后对应的字段名
    default-checked-keys:默认选中哪些项,它的值可以累加
    check-strictly:是否父子关联，默认是false关联  true:不关联

    setCheckedKeys：用法：this.$refs.el-tree的ref的值.setCheckedKeys([需要勾选相的值])
    getCheckedKeys:作用：获取已勾选的值  this.$refs.el-tree的ref的值.getCheckedKeys()它会把返回已勾选的值
 -->

    <el-tree
      ref="tree"
      check-strictly
      show-checkbox
      :data="treeData"
      node-key="id"
      :default-checked-keys="defaultKeys"
      :props="{ label: 'name' }"
    />

    <template #footer>
      <div style="text-align: center">
        <el-button @click="cancelEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleId, sysRoleAssignPrem } from '@/api/setting.js'
export default {
  data() {
    return {
      isShow: false,
      treeData: [],
      defaultKeys: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysPermission()
      this.treeData = changeData(res.data, '0')
      console.log('所有权限数据', res)
    },
    cancelEvent() {
      this.isShow = false
      this.$refs.tree.setCheckedKeys([])
    },
    // 获取角色详情
    async getRoleInfo(id) {
      this.id = id
      const res = await sysRoleId(id)
      // this.defaultKeys = res.data.permIds
      this.$refs.tree.setCheckedKeys(res.data.permIds)
      console.log('角色详情', res)
    },
    // 修改权限点
    async submit() {
      await sysRoleAssignPrem(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('修改权限成功')
      this.cancelEvent()
    }
  }
}
</script>
<style></style>
